#include <iostream>
#include <signal.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
using namespace std;

void handler(int signo)
{
    cout << "get a signo : " << signo << endl;
    pid_t id = 0;
    while(id = waitpid(-1, nullptr, WNOHANG))
    {
        if(id <= 0) break; //没有需要回收的子进程了!
        cout << "回收子进程 : " << id << endl;
    }
}

int main()
{
    signal(SIGCHLD, handler);
    for (int i = 0; i < 10; i++)
    {
        pid_t id = fork();
        if (id == 0)
        {
            // child
            cout << "child is running" << endl;
            sleep(5);
            exit(10);
        }
    }

    while(true)
    {
        sleep(1); // 父进程一直在进行某种服务
    }
    return 0;
}
